﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Region
{
    public class Groupe
    {
        protected Engine m_engine;
        protected bool m_read;
        protected Dictionary<String, bool> m_Access;
        private Int32 d_GroupeID;
        private String d_GroupeName, d_Access, d_AccessEx;
        protected bool readGroupe(Int32 GroupeID)
        {
            m_read = false;
            m_Access = new Dictionary<String, bool>();
            SQLResult r = m_engine.SQLSelect("SELECT * FROM groupe WHERE GroupeID=@GroupeID", new SQLParam() { { "@GroupeID", GroupeID } });
            if (r.Count == 0) return false;
            try
            {
                d_GroupeID = (Int32)r[0]["GroupeID"];
                d_GroupeName = (String)r[0]["GroupeName"];
                d_Access = (String)r[0]["Access"];
                d_AccessEx = (String)r[0]["AccessEx"];
            }
            catch { return false; }
            /*MySqlCommand cmd = m_engine.Command("SELECT * FROM groupe WHERE GroupeID=@GroupeID");
            cmd.Parameters.AddWithValue("@GroupeID", GroupeID);
            MySqlDataReader r = cmd.ExecuteReader();
            if (!r.Read()) return false;
            try
            {
                d_GroupeID = r.GetInt32("GroupeID");
                d_GroupeName = r.GetString("GroupeName");
                d_Access = r.GetString("Access");
                d_AccessEx = r.GetString("AccessEx");
                r.Close();
            }
            catch { return false;}
            finally { r.Close(); }*/

            { String[] A = d_Access.Split(','); Int32 n = A.Length; for (int i = 0; i < n; i++) m_Access[A[i]] = true; }
            { String[] A = d_AccessEx.Split(','); Int32 n = A.Length; for (int i = 0; i < n; i++) m_Access[A[i]] = true; }
            return true;
        }
        public Groupe(Engine engine, Int32 GroupeID)
        {
            m_read = false;
            m_engine = engine;
            readGroupe(GroupeID);
        }
        public bool isAccess(String Access) { return m_Access.ContainsKey(Access); }
        public Dictionary<String, bool> getAccess() { return new Dictionary<String, bool>(m_Access);}
    }
    
}
